﻿using Microsoft.Xna.Framework;
using ShevaEngine.Core.Foundation.GraphicsObjects;
using ShevaEngine.Core.Modules.Graphics;
using ShevaEngine.Core.Modules.Graphics.Cameras;

namespace ShevaEngine.Core.GameLogic.Triggers.Collision_Triggers
{
    /// <summary>
    /// Circle collision trigger.
    /// </summary>
    public class CircleCollisionTrigger : CollisionTrigger
    {
        /// <summary>Radius.</summary>
        public float Radius;
        /// <summary>Draw call key.</summary>
        private DrawCallKey DrawCallKey;
        /// <summary>Draw call.</summary>
        private DrawCall DrawCall;

        /// <summary>
        /// Constructor.
        /// </summary>
        public CircleCollisionTrigger(float radius)
            : base()
        {
            this.Radius = radius;

            this.DrawCallKey = Drawing.Instance.DrawCallKey;
            this.DrawCall = Drawing.Instance.CreateCircle();
        }

        /// <summary>
        /// Collidate.
        /// </summary>
        /// <param name="trigger1"></param>
        /// <param name="trigger2"></param>
        /// <returns></returns>
        public static bool Collide(CircleCollisionTrigger trigger1, CircleCollisionTrigger trigger2)
        {
            return Vector3.DistanceSquared(trigger1.World.Translation, trigger2.World.Translation) <=
                ((trigger1.Radius + trigger2.Radius) * (trigger1.Radius + trigger2.Radius));
        }

        /// <summary>
        /// Add in rendering pipeline.
        /// </summary>
        /// <param name="renderingPipeline"></param>
        /// <param name="camera"></param>
        public override void AddInRenderingPipeline(RenderingPipeline renderingPipeline, Camera camera)
        {
            renderingPipeline.AddOpaqueGraphicsObject(this.DrawCallKey, this.DrawCall, Matrix.CreateScale(this.Radius) * 
                Matrix.CreateTranslation(this.World.Translation));
        }
    }
}
